package flc.ast.dialog;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.core.DrawerPopupView;

import java.util.List;

import flc.ast.R;
import flc.ast.adapter.SortAdapter;
import flc.ast.bean.ResumeBean;
import flc.ast.utils.DragCallback;
import flc.ast.utils.ResumeDataUtils;

public class SortDrawer extends DrawerPopupView implements View.OnClickListener {

    public static int position;
    private RecyclerView recyclerView;
    private SortAdapter sortAdapter;
    private List<ResumeBean> listResume;

    public SortDrawer(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.drawer_sort;
    }

    @Override
    protected void onCreate() {
        findViewById(R.id.ivCancel).setOnClickListener(this);
        findViewById(R.id.ivConfirm).setOnClickListener(this);
        listResume = ResumeDataUtils.getResumeList();
        recyclerView = findViewById(R.id.rvModuleList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        sortAdapter = new SortAdapter(listResume.get(position).getResumeData());
        recyclerView.setAdapter(sortAdapter);
        // 绑定拖动功能
        ItemTouchHelper touchHelper = new ItemTouchHelper(new DragCallback(sortAdapter));
        touchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivCancel:
                dismiss();
                break;
            case R.id.ivConfirm:
                ResumeDataUtils.saveResumeList(listResume);
                dismiss();
                if (listener != null) {
                    listener.clickConfirm();
                }
                break;
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }

}
